package Blood.SqlParse;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/*
*
*desc: spider the sql files from abslute path recurse
*
* */
public class FileSpider {

   private List<String> sqlFiles = new ArrayList<String>();

   public List<String> getSqlFiles(){
       return sqlFiles ;
   }
   public List<String> getHiveSqlFiles(){
       return sqlFiles.stream().filter(x -> "hive".equals(x.split(",")[1])).collect(Collectors.toList());
   }
   public void spiderSqlFiles(String path){

     File files = new File(path);
     if(null == files.listFiles()){
        return;
     }
     for(File f : files.listFiles()){
        if(f.isDirectory()) {
            spiderSqlFiles(f.getPath());
        }else if(f.getName().endsWith(".sql")){
            sqlFiles.add(f.getAbsolutePath() + "," + getPlatform(f.getAbsolutePath().replace(f.getName(),"")
                    ,f.getName().replace(".sql","")));
        }
     }
   }
   public String getPlatform(String filePath,String sqlFileName){

       String encode = "UTF-8" ;
       String platFormType = null ;
       byte[] filecontent = null ;
       String content = null ;
       long filelength = 0 ;
       FileInputStream in = null ;
       String con = null ;
       for(File f : new File(filePath).listFiles()){
          if(f.isFile() && f.getName().equals(sqlFileName+".sh") ) {
            filelength = f.length();
            filecontent = new byte[(int) filelength];
              try {
                  in = new FileInputStream(f);
                  in.read(filecontent);
                  content = new String(filecontent,encode).replaceAll("#.*","");
                  if(content.contains(sqlFileName)
                          && content.contains("-f")
                          && (content.contains("presto")
                              || content.contains("hive")
                              || content.contains("clickhouse")
                              || content.contains("impala"))){

                     con = content.replaceAll("#.*\r","").replaceAll("--catalog hive","").replaceAll("hive.*?-e","") ;
//                      System.out.println(con.contains("presto")+f.toString());
                     platFormType = con.contains("presto")?
                                                   "presto":con.contains("clickhouse")?"clickhouse":
                                                                      con.contains("impala")?"impala"
                                                                              : ( con.contains("spark-sql")?"spark-sql":"hive");
                  }else{
                     // 处理 文件中不含 -f 的情况，
                  }

              } catch (IOException e) {
                  e.printStackTrace();
              }finally {
                  content = null ;
                  try {
                      in.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  in = null ;
              }
          }
       }
      return platFormType ;
   }
    public static void main(String[] args) {
       FileSpider fs = new FileSpider();
//       fs.spiderSqlFiles("F:\\yh\\yc_data\\workflow\\biz\\rpt\\biz-goods\\");
//       fs.getSqlFiles().forEach( x -> System.out.println(x));
        String rs = fs.getPlatform("F:\\yh\\yc_data\\workflow\\biz\\dws\\dws-goods\\last_update"
                ,"biz");
//        fs.getHiveSqlFiles().forEach(x-> System.out.println(x));
        System.out.println(rs);
    }


}
